package leecode

func nextLargerNodes(head *ListNode) []int {
	arr := make([]int, 0)
	for head != nil {
		arr = append(arr, head.Val)
		head = head.Next
	}
	//单调栈
	stack := make([]int, 0) //存放数组下标
	result := make([]int, len(arr))
	stack = append(stack, 0)
	for i := 1; i < len(arr); i++ {
		//取出栈顶元素的下标
		topIndex := stack[len(stack)-1]
		if arr[i] > arr[topIndex] {
			for len(stack) != 0 && arr[i] > arr[topIndex] {
				result[topIndex] = arr[i]
				stack = stack[:len(stack)-1]
				if len(stack) != 0 {
					topIndex = stack[len(stack)-1]
				}
			}
			stack = append(stack, i)
		} else {
			stack = append(stack, i)
		}
	}
	return result
}
